import { request, decodeURI } from '../utils'

export async function query (params) {
  if(params.keyword){
      const url = '/role/query?jsessionid=' + localStorage.getItem("token") + '&length=' + params.pageSize + '&offset=' + (params.current-1) + 
      '&field=' + params.field + '&keyword=' + params.keyword;
      return request(url, {
        method: 'get',
        data: params
      })
  }else{
      const url = '/role/query?jsessionid=' + localStorage.getItem("token") + '&length=' + params.pageSize + '&offset=' + (params.current-1)
      return request(url, {
        method: 'get',
        data: params
      })
  }
}
export async function byrole (params) {  
  const url = '/adminAccount/queryByRole?roleId=' + params.roleId+ '&jsessionid=' + localStorage.getItem("token") 
  return request(url, {
    method: 'get',
    data: JSON.stringify(params)
  })
}

export async function Accountquery (params) {
  if(params.keyword){
      const url = '/adminAccount/query?jsessionid=' + localStorage.getItem("token") + '&length=20' + '&offset=' + (params.current-1)
      return request(url, {
        method: 'get',
        data: params
      })
  }else{
      const url = '/adminAccount/query?jsessionid=' + localStorage.getItem("token") + '&length=20'
      return request(url, {
        method: 'get',
        data: params
      })
  }
}

export async function grantrole (params) {  
  const url = '/role/grantRoleList/'+params.roleId+'?jsessionid=' + localStorage.getItem("token")
  return request(url, {
    method: 'get',
    data: JSON.stringify(params)
  })
}
export async function listRole (params) {  
  const url = '/role/listRole/?jsessionid=' + localStorage.getItem("token")
  return request(url, {
    method: 'get',
    data: JSON.stringify(params)
  })
}

export async function create (params) {
  const url = '/role/add/?perId='+ params.perId +'&jsessionid=' + localStorage.getItem("token")
  return request(url, {
    method: 'post',
    headers: {'Accept': 'application/json','Content-Type': 'application/json'},
    body: JSON.stringify(params.roleEntry)
  })
}
export async function update (params) {
  const url = '/role/edit/?perId='+ params.perId +'&jsessionid=' + localStorage.getItem("token")
  return request(url, {
    method: 'put',
    headers: {'Accept': 'application/json','Content-Type': 'application/json'},
    body: JSON.stringify(params.role)
  })
}
export async function addUser (params) {
  return request('/adminAccount/add?&roleId='+ params.perId + '&jsessionid=' + localStorage.getItem("token"), {
    method: 'post',
    headers: {'Accept': 'application/json','Content-Type': 'application/json'},
    body: JSON.stringify(params.admin)
  })
}

export async function remove (params) {
  return request('/role/remove/' + params.id + '?jsessionid=' + localStorage.getItem("token"), {
    method: 'delete'
  })
}

export async function EditUser (params) {
  const url = '/adminAccount/edit/?jsessionid=' + localStorage.getItem("token")
  return request(url, {
    method: 'put',
    headers: {'Accept': 'application/json','Content-Type': 'application/json'},
    body: JSON.stringify(params)
  })
}
export async function removeUser (params) {
  return request('/adminAccount/remove/'+ params.id + '?jsessionid=' + localStorage.getItem("token"), {
    method: 'delete',
  })
}







